package com.ruoyi.project.jxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.jxy.domain.Corpus;
import com.ruoyi.project.jxy.domain.Entity;
import com.ruoyi.project.jxy.domain.SlotEntityRef;
import com.ruoyi.project.jxy.mapper.CorpusMapper;
import com.ruoyi.project.jxy.mapper.EntityMapper;
import com.ruoyi.project.jxy.mapper.SlotEntityRefMapper;
import com.ruoyi.project.jxy.service.ICorpusService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 语料,保存有每个技能对应的语料.Service业务层处理
 *
 * @author ruoyi
 * @date 2019-12-06
 */
@Service
@Transactional
public class CorpusServiceImpl extends ServiceImpl<CorpusMapper, Corpus> implements ICorpusService {
    @Resource
    private SlotEntityRefMapper slotEntityRefMapper;
    @Resource
    private EntityMapper entityMapper;

    @Override
    public boolean saveCourpusInfo(Corpus corpus) {
        corpus.setCreateTime(new Date());
        corpus.setCreateUser(SecurityUtils.getUsername());
        //替换后的语料
        String slots = corpus.getContent();
        Pattern p = Pattern.compile("\\{([^}]*)\\}");
        Matcher m = p.matcher(corpus.getContent());
        List<Map> lst = slotEntityRefMapper.getSlotCodeByIntentionId(corpus.getIntentionId());
        //槽位实体对应关系map
        Map<String, String> slotCodeMap = new HashMap();
        if (lst != null && lst.size() > 0) {
            for (Map map :
                    lst) {
                slotCodeMap.put(map.get("slot").toString(), map.get("code").toString());
            }
        }
        while (m.find()) {
            //槽位
            String slot = m.group(1);
            //检查该意图下槽位是否存在
            QueryWrapper wrapper = new QueryWrapper();
            //槽位
            wrapper.eq("slot", slot);
            wrapper.eq("intention_id", corpus.getIntentionId());
            SlotEntityRef checkResult = slotEntityRefMapper.selectOne(wrapper);
            //存在
            if (checkResult != null) {
                System.out.println("该槽位存在对应关系");
                //引用次数+1
                checkResult.setSlotRefTimes(checkResult.getSlotRefTimes() + 1);
                checkResult.setUpdateUser(SecurityUtils.getUsername());
                checkResult.setUpdateTime(new Date());
                slotEntityRefMapper.updateById(checkResult);
                String code = slotCodeMap.get(slot);
                if(StringUtils.isNotBlank(code)){
                    slots = slots.replaceAll("\\{" + slot + "}", "{" + code + "}");
                }
                //slots = slots.replaceAll("\\{" + slot + "}", "{" + entity.getCode() + "}");
            } else {
                //
                SlotEntityRef slotEntityRef = new SlotEntityRef();
                slotEntityRef.setSlotRefTimes(1L);
                //槽位类型默认是实体
                slotEntityRef.setType("0");
                //槽位
                slotEntityRef.setSlot(slot);
                slotEntityRef.setIntentionId(corpus.getIntentionId());
                slotEntityRef.setCreateTime(new Date());
                slotEntityRef.setCreateUser(SecurityUtils.getUsername());
                slotEntityRefMapper.insert(slotEntityRef);
            }
        }
        corpus.setSolts(slots);
        baseMapper.insert(corpus);
        return true;
    }

    @Override
    public boolean removeCorpusInfo(Long[] ids) {
        List<Corpus> Corpuss = baseMapper.selectBatchIds(Arrays.asList(ids));
        for (Corpus corpus : Corpuss) {
            Pattern p = Pattern.compile("\\{([^}]*)\\}");
            Matcher m = p.matcher(corpus.getContent());
            while (m.find()) {
                //槽位
                String slot = m.group(1);
                //检查该意图下槽位是否存在
                QueryWrapper wrapper = new QueryWrapper();
                //槽位
                wrapper.eq("slot", slot);
                wrapper.eq("intention_id", corpus.getIntentionId());
                SlotEntityRef checkResult = slotEntityRefMapper.selectOne(wrapper);
                if (checkResult != null) {
                    //引用次数大于1 ,减小引用次数
                    if (checkResult.getSlotRefTimes() > 1) {
                        checkResult.setSlotRefTimes(checkResult.getSlotRefTimes() - 1);
                        slotEntityRefMapper.updateById(checkResult);
                    } else {
                        //引用次数小于1
                        slotEntityRefMapper.deleteById(checkResult.getId());
                    }
                }
            }
        }
        //删除语料
        baseMapper.deleteBatchIds(Arrays.asList(ids));
        return true;
    }
}
